package cn.v2tila.rsosa.student.service.serviceImpl;

import cn.v2tila.rsosa.student.entity.DeliveryEntity;
import cn.v2tila.rsosa.student.jpa.DeliveryJPA;
import cn.v2tila.rsosa.student.service.DeliveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author v2tila on 1:07 2019/3/29
 */
@Service
public class DeliveryServiceImpl implements DeliveryService {

    @Autowired
    private DeliveryJPA deliveryJPA;

    @Override
    public DeliveryEntity saveAndFlush(DeliveryEntity deliveryEntity) {
        return deliveryJPA.saveAndFlush(deliveryEntity);
    }

    @Override
    public boolean existsByUniqueIdAndZpxxId(String uniqueId, Integer zpxxId) {
        return deliveryJPA.existsByUniqueIdAndZpxxId(uniqueId, zpxxId)  ;
    }

    @Override
    public List<DeliveryEntity> findByUniqueId(String uniqueId) {
        return deliveryJPA.findByUniqueId(uniqueId);
    }

    @Override
    public Page<DeliveryEntity> findByAssoIdAndCode(Integer assoId, Integer code,int page) {
        Sort sort = new Sort(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(page, 10, sort);

        return deliveryJPA.findByAssoIdAndCode(assoId,code,pageable);
    }

    @Override
    public boolean existsByAssoIdAndCode(Integer assoId, Integer code) {
        return deliveryJPA.existsByAssoIdAndCode(assoId, code);
    }

    @Override
    public DeliveryEntity findById(int id) {
        Optional<DeliveryEntity> findResult = deliveryJPA.findById(id);
        return findResult.get();
    }
}
